package com.gl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gl.entity.StatisticsDaily;
import com.gl.entity.UcenterMember;
import com.gl.mapper.StatisticsDailyMapper;
import com.gl.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gl.service.UcenterMemberService;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-12-14
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterMemberService memberService;

    //统计某一天的数据
    @Override
    public void statisticsDay(String day) {

        //重新统计时删除数据库中这一天统计的数据
        QueryWrapper<StatisticsDaily> wrapperMember = new QueryWrapper<>();
        wrapperMember.eq("date_calculated",day);
        baseMapper.delete(wrapperMember);

        //统计注册人数
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.like("gmt_create",day);
        int count = memberService.count(wrapper);


        StatisticsDaily statisticsDaily = new StatisticsDaily();
        statisticsDaily.setRegisterNum(count);//注册人数
        statisticsDaily.setDateCalculated(day);//统计的日期
        //其他人数模拟
        statisticsDaily.setVideoViewNum(RandomUtils.nextInt(100,200));
        statisticsDaily.setCourseNum(RandomUtils.nextInt(100,200));
        statisticsDaily.setLoginNum(RandomUtils.nextInt(100,200));

        //向表里添加统计的记录
        baseMapper.insert(statisticsDaily);

    }
    //统计某一时间到某一时间的图表数据
    @Override
    public Map<String, Object> statisticsChart(String begin, String end) {
        Map<String,Object> map = new HashMap<>();

        //查询这一段时间的所有统计数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated",begin,end);
        List<StatisticsDaily> statisticsDailies = baseMapper.selectList(wrapper);


        List<String> dateList = new ArrayList<>();//日期集合
        List<Integer> registerList = new ArrayList<>();//注册集合
        List<Integer>  loginList = new ArrayList<>();//登录集合
        List<Integer> videoplayList = new ArrayList<>();//播放视频集合
        List<Integer> newCourseList = new ArrayList<>();//新增课程集合

        //遍历添加日期对应的数据
        for (StatisticsDaily statisticsDaily : statisticsDailies) {
            dateList.add(statisticsDaily.getDateCalculated());
            registerList.add(statisticsDaily.getRegisterNum());
            loginList.add(statisticsDaily.getLoginNum());
            videoplayList.add(statisticsDaily.getVideoViewNum());
            newCourseList.add(statisticsDaily.getCourseNum());
        }
        //封装数据到map里
        map.put("dateList",dateList);
        map.put("registerList",registerList);
        map.put("loginList",loginList);
        map.put("videoplayList",videoplayList);
        map.put("newCourseList",newCourseList);
        return map;
    }
}
